{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ef96a242",
   "metadata": {},
   "source": [
    "导入numpy库并取别名为np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "77143c21",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd1d0869",
   "metadata": {},
   "source": [
    "打印输出numpy的版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9cba604a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.2.3'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f00c44c9",
   "metadata": {},
   "source": [
    "创建长度为10的零向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fb3b0dc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zero_vector = np.zeros(10)\n",
    "zero_vector"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a02a8ad7",
   "metadata": {},
   "source": [
    "获取数组所占内存大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dfb0a5ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3])\n",
    "arr.nbytes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32195d55",
   "metadata": {},
   "source": [
    "怎么用命令行获取numpy add函数的文档说明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "718c8b83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on ufunc in module numpy:\n",
      "\n",
      "add = <ufunc 'add'>\n",
      "    add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature])\n",
      "\n",
      "    Add arguments element-wise.\n",
      "\n",
      "    Parameters\n",
      "    ----------\n",
      "    x1, x2 : array_like\n",
      "        The arrays to be added.\n",
      "        If ``x1.shape != x2.shape``, they must be broadcastable to a common\n",
      "        shape (which becomes the shape of the output).\n",
      "    out : ndarray, None, or tuple of ndarray and None, optional\n",
      "        A location into which the result is stored. If provided, it must have\n",
      "        a shape that the inputs broadcast to. If not provided or None,\n",
      "        a freshly-allocated array is returned. A tuple (possible only as a\n",
      "        keyword argument) must have length equal to the number of outputs.\n",
      "    where : array_like, optional\n",
      "        This condition is broadcast over the input. At locations where the\n",
      "        condition is True, the `out` array will be set to the ufunc result.\n",
      "        Elsewhere, the `out` array will retain its original value.\n",
      "        Note that if an uninitialized `out` array is created via the default\n",
      "        ``out=None``, locations within it where the condition is False will\n",
      "        remain uninitialized.\n",
      "    **kwargs\n",
      "        For other keyword-only arguments, see the\n",
      "        :ref:`ufunc docs <ufuncs.kwargs>`.\n",
      "\n",
      "    Returns\n",
      "    -------\n",
      "    add : ndarray or scalar\n",
      "        The sum of `x1` and `x2`, element-wise.\n",
      "        This is a scalar if both `x1` and `x2` are scalars.\n",
      "\n",
      "    Notes\n",
      "    -----\n",
      "    Equivalent to `x1` + `x2` in terms of array broadcasting.\n",
      "\n",
      "    Examples\n",
      "    --------\n",
      "    >>> import numpy as np\n",
      "    >>> np.add(1.0, 4.0)\n",
      "    5.0\n",
      "    >>> x1 = np.arange(9.0).reshape((3, 3))\n",
      "    >>> x2 = np.arange(3.0)\n",
      "    >>> np.add(x1, x2)\n",
      "    array([[  0.,   2.,   4.],\n",
      "           [  3.,   5.,   7.],\n",
      "           [  6.,   8.,  10.]])\n",
      "\n",
      "    The ``+`` operator can be used as a shorthand for ``np.add`` on ndarrays.\n",
      "\n",
      "    >>> x1 = np.arange(9.0).reshape((3, 3))\n",
      "    >>> x2 = np.arange(3.0)\n",
      "    >>> x1 + x2\n",
      "    array([[ 0.,  2.,  4.],\n",
      "           [ 3.,  5.,  7.],\n",
      "           [ 6.,  8., 10.]])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.add) # add不能加括号"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ffcf145",
   "metadata": {},
   "source": [
    "创建一个长度为10的零向量，并把第五个值赋值为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2ca1d126",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zeros_10 = np.zeros(10)\n",
    "zeros_10[4] = 1\n",
    "zeros_10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a079a603",
   "metadata": {},
   "source": [
    "创建一个值域为10到49的等差数列的向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7cab8703",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n",
       "       27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,\n",
       "       44, 45, 46, 47, 48, 49])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10, 50)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "433ba6e0",
   "metadata": {},
   "source": [
    "将一个向量进行反转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "37bbccf6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 4, 3, 2, 1])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1, 2, 3, 4, 5])[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd387b47",
   "metadata": {},
   "source": [
    "\n",
    "创建一个3x3的等差矩阵，值域为0到8\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a4cbfa27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(9).reshape(3, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af0f040e",
   "metadata": {},
   "source": [
    "从数组[1, 2, 0, 0, 4, 0]中找出非0元素的位置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "aec93ede",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 1, 4]),)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_1 = np.array([1, 2, 0, 0, 4, 0])\n",
    "np.where(arr_1)\n",
    "# np.where(arr_1 != 0 )\n",
    "# np.nonzero(arr_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28bfe894",
   "metadata": {},
   "source": [
    "创建一个3x3的单位矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2dcc0e16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "849549c0",
   "metadata": {},
   "source": [
    "\n",
    "创建一个3x3的全1矩阵\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b6df3399",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((3, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25ca14d8",
   "metadata": {},
   "source": [
    "\n",
    "创建一个3x3x3的随机数组（0-1之间均匀分布）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8f7a4bec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.15870708, 0.21811   , 0.91809427],\n",
       "        [0.25492334, 0.22556737, 0.02260889],\n",
       "        [0.6788591 , 0.28800662, 0.83390819]],\n",
       "\n",
       "       [[0.63196579, 0.31000084, 0.25040001],\n",
       "        [0.33925244, 0.18211031, 0.01428156],\n",
       "        [0.7283481 , 0.92771648, 0.91619739]],\n",
       "\n",
       "       [[0.87603975, 0.68725917, 0.47705755],\n",
       "        [0.79947637, 0.77587359, 0.80471747],\n",
       "        [0.26469212, 0.45433092, 0.21535418]]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(3, 3, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df4d2120",
   "metadata": {},
   "source": [
    "\n",
    "创建一个10x10的随机数组，并找出该数组中的最大值与最小值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a7a8363e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最大值：0.9973015910360002，最小值：0.004521075804372665\n"
     ]
    }
   ],
   "source": [
    "arr_10_10 = np.random.rand(10, 10)\n",
    "max_value = arr_10_10.max()\n",
    "min_value = arr_10_10.min()\n",
    "print(f'最大值：{max_value}，最小值：{min_value}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "137fde09",
   "metadata": {},
   "source": [
    "\n",
    "创建一个长度为30的随机向量，并求它的平均值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dcf07443",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.4813438160065296)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(30).mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98ccafa7",
   "metadata": {},
   "source": [
    "\n",
    "创建一个2维数组，该数组边界值为1，内部的值为0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "52ab6999",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 1.],\n",
       "       [1., 0., 0., 0., 1.],\n",
       "       [1., 0., 0., 0., 1.],\n",
       "       [1., 0., 0., 0., 1.],\n",
       "       [1., 1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_1 = np.ones((5,5))\n",
    "arr_1[1:-1, 1:-1] = 0\n",
    "arr_1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1df58fa6",
   "metadata": {},
   "source": [
    "\n",
    "创建一个8x8的国际象棋棋盘矩阵（黑块为0，白块为1）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "10c691bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 1, 0, 1, 0, 1, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chessBoard = np.zeros((8, 8), dtype=int)\n",
    "chessBoard[::2, ::2] = 1 # 偶数索引行, 偶数索引格为白\n",
    "chessBoard[1::2, 1::2] = 1 # 奇数索引行, 奇数索引格为白\n",
    "chessBoard"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe165c95",
   "metadata": {},
   "source": [
    "对5x5的随机矩阵进行归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "35725791",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始矩阵：\n",
      "[[2 3 2 3 2]\n",
      " [1 4 3 4 4]\n",
      " [2 1 2 4 5]\n",
      " [5 2 3 3 5]\n",
      " [1 1 4 3 4]]\n",
      "归一化矩阵：\n",
      "[[0.25 0.5  0.25 0.5  0.25]\n",
      " [0.   0.75 0.5  0.75 0.75]\n",
      " [0.25 0.   0.25 0.75 1.  ]\n",
      " [1.   0.25 0.5  0.5  1.  ]\n",
      " [0.   0.   0.75 0.5  0.75]]\n"
     ]
    }
   ],
   "source": [
    "matrix_5_5_rand = np.random.randint(1, 6, size=(5, 5))\n",
    "print(f'原始矩阵：\\n{matrix_5_5_rand}')\n",
    "\n",
    "normalized_matrix = (matrix_5_5_rand - matrix_5_5_rand.min()) / (matrix_5_5_rand.max() - matrix_5_5_rand.min())\n",
    "print(f'归一化矩阵：\\n{normalized_matrix}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
